package com.isoftstone.role.controller;


import com.isoftstone.platform.common.uitl.DataUtil;
import com.isoftstone.platform.controller.BaseController;
import com.isoftstone.platform.entity.PadingRstType;
import com.isoftstone.platform.entity.PagingBean;
import com.isoftstone.role.entity.RoleEntity;
import com.isoftstone.role.entity.RoleMenuInfoTreeEntity;
import com.isoftstone.role.entity.SearchRoleEntity;
import com.isoftstone.role.service.RoleService;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    //工厂方法  访问页面
    Logger logger = Logger.getLogger(RoleController.class);

    @Resource
    private RoleService roleService;
    @RequiresPermissions("cldk_data_role")
    @RequestMapping(value = "/init")
    public String initRole(PagingBean pagingBean) {
        logger.info("");

        return "role/roleList";
    }
    @RequiresPermissions("cldk_data_role")
    @RequestMapping("/getRoleList")
    @ResponseBody
    public PadingRstType<RoleEntity> getClassInfolist(SearchRoleEntity search, PagingBean pagingBean){
        // request.getParameter("parentId");
        logger.info(search);
        PadingRstType<RoleEntity> padingRstType = roleService.getRoleList(search, pagingBean);
        return padingRstType;
    }


    //getRoleList
    @RequiresPermissions("cldk_data_role")
    @RequestMapping(value = "/addRole")
    @ResponseBody
    //才能返回对应的json字符串
    public String addRole(RoleEntity roleEntity) {
        logger.info(roleEntity);
        String uuid =  DataUtil.getUUID().toString().replaceAll("-","");
        roleEntity.setRoleUuid(uuid);
        roleService.addRole(roleEntity);
        return buildSuccessJsonMsg("role.add.sucess", null);
    }
    @RequiresPermissions("cldk_data_role")
    @RequestMapping(value = "/deleteRoleInfo")
    @ResponseBody
    public String deleteRoleInfo(@RequestParam("roleuuid") String roleuuid) {
        logger.info(roleuuid);
        String msg = roleService.deleteRoleInfo(roleuuid);
        return buildSuccessJsonMsg(msg, null);
    }

    //getRoleList
    @RequiresPermissions("cldk_data_role")
    @RequestMapping(value = "/updateRoleDail")
    @ResponseBody
    //才能返回对应的json字符串
    public String updateRoleDail(RoleEntity roleEntity) {
        logger.info(roleEntity);
        roleService.updateRoleDail(roleEntity);
        return buildSuccessJsonMsg("role.update.sucess", null);
    }

    @RequiresPermissions("cldk_data_role")
    @RequestMapping("/queryRoleMenuInfoZtreeList")
    @ResponseBody
    public List<RoleMenuInfoTreeEntity> queryRoleMenuInfoZtreeList(String roleuuid){
        List<RoleMenuInfoTreeEntity> list = roleService.queryRoleMenuInfoZtreeList(roleuuid);
        return list;
    }
    ///grantRole
    @RequiresPermissions("cldk_data_role")
    @RequestMapping(value = "/grantRole")
    @ResponseBody
    //才能返回对应的json字符串
    public String grantRole(@RequestParam("roleuuid")String roleuuid,@RequestParam("mid") String[] mid) {
        logger.info(roleuuid);
        logger.info(mid);
        if(mid.length==0){
            roleService.cancelPermissions(roleuuid);
            return buildSuccessJsonMsg("role.cancel.sucess", null);
        }
        roleService.cancelPermissions(roleuuid);
        roleService.grantRole(roleuuid,mid);
        return buildSuccessJsonMsg("role.grant.sucess", null);
    }


}
